package com.check.controller;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.PageObject;
import com.check.pojo.WorkRole;
import com.check.service.WorkRoleService;
import com.check.util.DateUtils;

@Controller
public class WorkRoleController extends BaseController{

	@Autowired
	@Qualifier("workRoleService")
	private WorkRoleService workRoleService;
	
	@RequestMapping("workRoleQuery.do")
	public ModelAndView workRoleQuery(HttpServletRequest request,HttpServletResponse response,
				@ModelAttribute("pageObject") PageObject pageObject ){
		if(pageObject.getCurrent()==0){
			pageObject.setCurrent(1);
		}
		List<WorkRole> workRoleList = workRoleService.findAllWorkRole(pageObject);
		pageObject.setTotal(workRoleService.findWorkRoleCount());
		pageObject.setUrl("workRoleQuery.do");
		saveSysLog(RE_WORKROLE, LOG_QUERY, request);
		return new ModelAndView("page/workrole/workRoleList")
					.addObject("workRoleList",workRoleList)
					.addObject("path", request.getContextPath())
					.addObject("page",pageObject.toPage());
	}
	
	@RequestMapping("toAddWorkRole.do")
	public ModelAndView toAddWorkRole(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("page/workrole/workRoleAdd")
					.addObject("path",request.getContextPath());
	}
	
	
	@RequestMapping("findWorkRole.do")
	public void findWorkRole(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out=null;
		String w_id=request.getParameter("w_id");
		String flag = "0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			WorkRole findWorkRole = workRoleService.findByWorkRoleID(w_id);
			if(findWorkRole!=null){
				flag="1";
			}else{
				flag="0";
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	
	@RequestMapping(value="addWorkRole.do",method = RequestMethod.POST)
	public void addWorkRole(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("workRole") WorkRole workRole) {
		PrintWriter out = null;
		String flag = "0";
		
		try {
			out = response.getWriter();
			workRole.setW_name(URLDecoder.decode(request.getParameter("w_name"),  "UTF-8"));
			saveSysLog(RE_WORKROLE, LOG_ADD, request);
			if(workRoleService.insertWorkRole(workRole)){
				flag = "1";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
	
	@RequestMapping("toUpdWorkRole.do")
	public ModelAndView toUpdWorkRole(HttpServletRequest request,HttpServletResponse response) {
		String w_id=request.getParameter("w_id");
		WorkRole workRole = workRoleService.findByWorkRoleID(w_id);
		return new ModelAndView("page/workrole/workRoleUpdate")
		           .addObject("path", request.getContextPath())
		           .addObject("workRole", workRole);
	}
	
	@RequestMapping("updateWorkRole.do")
	public void updateWorkRole(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("workRole") WorkRole workRole) {
		PrintWriter out = null;
		String flag = "0";
		
		try {
			out = response.getWriter();
			workRole.setW_name(URLDecoder.decode(request.getParameter("w_name"),"UTF-8"));
			if(workRoleService.updateWorkRole(workRole)){
				flag = "1";
			}
			saveSysLog(RE_WORKROLE, LOG_UPDATE, request);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
	
	@RequestMapping("deleteWorkRole.do")
	public void deleteWorkRole(HttpServletRequest request,HttpServletResponse response) {
		String w_id=request.getParameter("w_id");
		PrintWriter out = null;
		String flag = "0";
		
		try {
			out = response.getWriter();
			if(workRoleService.deleteWorkRole(w_id)){
				flag = "1";
			}
			saveSysLog(RE_WORKROLE, LOG_DELETE, request);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
}
